<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ include file="/WEB-INF/decorators/taglibs.jsp"%>
<div class="w1a">
	<div class="w2">資料維護 » 承辦資料</div>
	<br />
	<table id="dg" class="easyui-datagrid"
		style="height: 600px; width: 800px;"
		url="${appPath}/inCharge/list.json" striped="true" title="承辦資料"
		data-options="
		singleSelect: true,
        method: 'post',
        toolbar: '#tb',
		">
		<thead>
			<tr>
				<th field="telNo" width="100">分機</th>
				<th field="name" width="150">承辦</th>
				<th field="sectionChief" width="150">科長</th>
				<!-- <th field="action" width="120" formatter="action">作業</th> -->
			</tr>
		</thead>
	</table>
	<div id="tb" style="height: auto">
		<a href="javascript:void(0)" class="easyui-linkbutton"
			onclick="loadData()">更新資料</a>
	</div>
</div>
<script>
	var editIndex = undefined; // 目前編輯的row index
	function endEditing() {
		if (editIndex == undefined) {
			return true;
		}
		if ($("#dg").datagrid('validateRow', editIndex)) {
			$('#dg').datagrid('endEdit', editIndex);
			$('#dg').datagrid('cancelEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			/*取消編輯狀態*/
			var ed = $("#dg").datagrid("getEditor", {
				index : editIndex,
				field : "code"
			});
			var fd = $("#dg").datagrid("getEditor", {
				index : editIndex,
				field : "name"
			});
			if ($(ed.target).text('getValue') == ""
					|| $(fd.target).text('getValue') == "") {
				$('#dg').datagrid('endEdit', editIndex);
				$('#dg').datagrid('cancelEdit', editIndex);
				return true;
			}
			return false;
		}
	}
	function onBeforeLoad(param) {
		$.each($('#searchForm').serializeArray(), function(i, v) {
			param[v.name] = v.value;
		});
	}
	function onClickCell(index, field, value) {
		if (field != 'action') {
			if (endEditing()) {
				$("#dg").datagrid("selectRow", index).datagrid('beginEdit',
						index);
				editIndex = index;
			} else {
				$("#dg").datagrid("selectRow", editIndex);
			}
		}
	}
	function onBeforeEdit(index, row, changes) {
		row.editing = true;
		$("#dg").datagrid("refreshRow", index);
	}
	function onAfterEdit(index, row, changes) {
		row.editing = false;
		$("#dg").datagrid("refreshRow", index);
		if (Object.keys(changes).length > 0) {
			update(index);
		}
	}
	function onCancelEdit(index, row, changes) {
		row.editing = false;
		$("#dg").datagrid("refreshRow", index);
	}
	function appendRow() {
		/* 初始提供一空白行供新增 */
		$("#dg").datagrid("appendRow", {
			name : "",
		});
	}
	function onLoadSuccess(data) {
		appendRow();
		if (editIndex != undefined) { /* 如果為繼續，於reload後將該行設為editable */
			$("#dg").datagrid('selectRow', editIndex).datagrid('beginEdit',
					editIndex);
		}
	}
	function loadData() {
		$.ajax({
			url :"${appPath}/users/load.json",
			type : 'get',
			async : false,
			cache: false,
			error : function(xhr) {
				alert("更新失敗!");
			},
			success : function(response) {
				if (eval(response)) {
					$("#dg").datagrid("load");
					alert("更新完成!");
				} else {
					alert("更新失敗!");
				}
			}
		});
	}
	function getRowIndex(target) {
		var tr = $(target).closest("tr.datagrid-row");
		return parseInt(tr.attr("datagrid-row-index"));
	}
	function cancel(target) {
		$("#dg").datagrid("cancelEdit", getRowIndex(target));
		editIndex = undefined;
	}
	function del(target) {
		$.messager.confirm("Confirm", "確定刪除?", function(r) {
			if (r) {
				var index = getRowIndex(target);
				var o = $("#dg").datagrid("getData").rows[index];
				$.post("${appPath}/codeType/delete/" + o.code + ".json",
						function(data) {
							console.log(data);
							if (eval(data)) {
								$("#dg").datagrid("deleteRow", index);
								if (editIndex > index) {
									editIndex -= 1;
								}
							} else {
								alert("刪除失敗!");
							}
							if (editIndex == index) {
								editIndex = undefined;
							}
						});
			}
		});
	}
	function action(val, row, ix) {
		var str = "";
		var o = $("#dg").datagrid("getData").rows[ix];
		if (o.code != undefined) {
			str = str.concat("[<a href='#do' onclick=\"del(this);\" >刪除</a>]");
		}
		if (row.editing) {
			str = str
					.concat("[<a href='#do' onclick=\"cancel(this);\" >取消</a>]");
		}
		return str;
	}
</script>